<?php

namespace V6;
class  C_RestPassword extends \ResourceClass
{

    public $isActive = true;
    public $remark = '重置密码';

    public function put(array $req, array $post, &$result)
    {
        try {
            if (!isset($req['uid'])) {
                return 'require user id ';
            }
            if (!isset($req['userType'])) {
                return 'require user type';
            }
            if (!isset($post['payload'])) {
                return 'require payload';
            }
            $data = base64_decode($post['payload']);
            @parse_str($data,$data);
            if(!isset($data['newpassward'])){
                return 'require message newpassward';
            }
            $newpassward = preg_replace('/[(\xc2\xa0)|\s]+/','', $data['newpassward']);
            if($newpassward == ''){
                return '密码不能为空';
            }
            $newpassward_count = strlen($newpassward);
            if($newpassward_count < 0){
                return '您的密码长度过低';
            }
            if($newpassward_count > 30){
                return '您的密码超出30位';
            }
            if(!isset($data['token'])){
                return 'require message token';
            }
            $customer_handler = new \Biz\UserHandler();
            $customer_cls = $customer_handler ->getById($req['userType'],$req['uid']);
            if(!is_object($customer_cls) || $customer_cls->getEnterpriseId() ==false){
                return 'data error';
            }
            $token = md5('XIANQIAO520'.$customer_cls->getId().'XIANQIAO520');   //生成令牌
            if($token == $data['token']){   //判断令牌
                $password = md5($newpassward);
                $user_cls = \Biz\UserHandler::getById($req['userType'], $req['uid']);
                $user_cls->setPassword($password);
                if (\Biz\UserHandler::update($req['userType'], $user_cls) == true) {
                    $result['uid'] = $customer_cls->getId();
                }else{
                    return '更新失败';
                }
            }else{
                return 'TOKEN验证失败';
            }
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }
}